{% extends base %}

{% block head %}
<style type="text/css">
p {
    margin: 0px;
}
</style>
{% end %}

{% block body %}

{% init action = "" %}
{% init path = "" %}
{% init index_size = 0 %}
{% init show_index_dirs = True %}

<div class="card">
    <div class="card-title">
        <span>文件索引</span>

        <div class="float-right">
            {% include common/button/back_button.html %}
        </div>
    </div>
</div>

<div class="vueApp">
<div class="card">
    <p>索引数量: {{index_size}}</p>

    {% if action == "reindex" %}
        <p>重建索引耗时: {{"%.2f"%cost}}秒</p>
    {% end %}
    
    <div class="row">
        <input type="hidden" name="action" value="reindex">
        {% if path != "" %}
        <div class="row">
            <label>文件路径</label>
            <input type="path" name="path" value="{{path}}">
        </div>
        {% end %}
        <div class="row">
            <button class="rebuild-index-btn">重建索引</button>
        </div>
    </div>
</div>

{% if show_index_dirs %}
<div class="card">
    <div class="card-title">
        <span>索引目录</span>
    </div>

    <div class="info light">一行一个路径</div>
    
    <textarea name="index_config" class="row" rows=10>{{"\n".join(index_dirs)}}</textarea>
    <input type="hidden" name="action" value="config">
    <button class="update-index-dirs">更新索引目录</button>
</div>
{% end %}

</div>

<script type="text/javascript">

$(".rebuild-index-btn").click(function () {
    var rebuildParams = {
        action: "reindex",
        path: xnote.getUrlParam("path"),
        is_ajax: true
    };
    $.post("/fs_index", rebuildParams, function (resp) {
        if (resp.code == "success") {
            xnote.toast("重建中...");
        } else {
            xnote.alert(resp.message);
        }
    }).fail(function (err) {
        xnote.alert("重建失败，请稍后重试");
    });
});

$(function () {
    $("body").on("click", ".update-index-dirs", function (e) {
        var index_config = $("[name=index_config]").val();
        var params = {"index_config": index_config};
        $.post("?action=config", params, function (e) {
            xnote.toast("更新成功");
            setTimeout(function () {
                window.location.reload();                    
            }, 500);
        }).fail(function (e) {
            console.error("更新失败", e);
            xnote.alert("更新失败");
        });
    });

    $(".rebuild-index-btn").click(function (e) {

    });
});
</script>
{% end %}